from django.conf import settings
from django.db import models

from itsdangerous import TimedJSONWebSignatureSerializer as Serializer, BadData


class BaseModel(models.Model):
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="最后一次修改时间")

    class Meta:
        abstract = True  # 抽象类，志勇于继承


def generate_save_user_token(openid):
    serializer = Serializer(settings.SECRET_KEY, 300)

    data = {
        "openid": openid
    }

    token = serializer.dumps(data)
    token = token.decode()
    print(token, 111111)
    return token

# 对ｏｐｅｎｉｄ进行解密


def check_save_secret_openid(access_token):
    print(access_token)
    serializer = Serializer(settings.SECRET_KEY, 300)
    try:
        data = serializer.loads(access_token)
    except BadData:
        return None
    print(data)
    print(111111)
    return data["openid"]

